﻿using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing.Design;
using UtfprLibrary.ComponentModel;
using UtfprLibrary.Windows.Forms.ComponentModel;
using UtfprLibrary.ComponentModel.Security;

namespace UtfprLibrary.Windows.Forms {
    /// <summary>
    /// The base Form of UtfprLibrary, all the other Forms extends from it.
    /// </summary>
    public partial class UtfprFormBase : Form, IUtfprInitialControlInFocus, IUtfprControl, IUtfprControlProvider {
        #region InitialControlInFocus

        private Control _initialControlInFocus = null;
        [Category("Behavior")]
        [Description("Initial control to be in focus.")]
        [DefaultValue(null)]
        [TypeConverter(typeof(InitialControlInFocusConverter))]
        public Control InitialControlInFocus {
            get { return _initialControlInFocus; }
            set {
                if (_initialControlInFocus != null && !UtfprControlBase.IsInDesignMode) {
                    _initialControlInFocus = value;
                    _initialControlInFocus.Focus();
                }
            }
        }

        #endregion

        #region UtfprControlService (IUtfprControl, IUtfprControlProvider)

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool UtfprControlEnabled {
            get { return this.Enabled; }
            set { this.Enabled = value; }
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool UtfprControlVisible {
            get { return this.Visible; }
            set { this.Visible = value; }
        }

        private IUtfprKeyName _functionOrOperation = null;
        [Category("Security"), Description("If IsFunction is true, loads a Function, otherwise loads an Operation")]
        [Editor("UtfprLibrary.Windows.Forms.ComponentModel.UtfprRestrictionEditor, UtfprLibrary.WindowsForms.Design", typeof(UITypeEditor))]
        public IUtfprKeyName FunctionOrOperationRestriction {
            get { return _functionOrOperation; }
            set { _functionOrOperation = value; }
        }

        private UtfprControlBase _utfprControlBase;
        [Browsable(false)]
        public UtfprControlBase UtfprControlBase {
            get {
                if (_utfprControlBase == null) {
                    _utfprControlBase = new UtfprControlBase(this);
                }
                return _utfprControlBase;
            }
        }

        #endregion
        
        public UtfprFormBase() {
            InitializeComponent();
        }
    }
}